use crate::tools::common;
use crate::tools::{
    pass::{self, PassItem},
    todo, urls,
};
use log::{error, warn};
use tauri_plugin_notification::NotificationBuilder;

#[tauri::command]
pub fn get_all_url_class() -> Vec<urls::UrlsClass> {
    match urls::get_url_class() {
        Ok(all_class) => all_class,
        Err(e) => {
            error!("查询分类出错!{:?}", e);
            Vec::new()
        }
    }
}
#[tauri::command]
pub fn get_url_by_class(c_id: &str) -> Vec<urls::Urls> {
    println!("c_id:{}", c_id);
    match urls::get_url(c_id) {
        Ok(urls) => urls,
        Err(e) => {
            error!("查询具体分类下的url出错!{:?}", e);
            vec![]
        }
    }
}
#[tauri::command]
pub fn get_all_url(c_id: &str, key_word: &str) -> Vec<urls::Urls> {
    // let context = tauri::generate_context!("tauri.conf.json");
    // Notification::new(&context.config().tauri.bundle.identifier)
    //     .title("Tauri")
    //     .body("Tauri is awesome!")
    //     .show()
    //     .unwrap();
    match urls::get_all_url(c_id, key_word) {
        Ok(urls) => urls,
        Err(e) => {
            error!("查询所有Url出错!{:?}", e);
            vec![]
        }
    }
}
#[tauri::command]
pub fn save_url_form(address_id: &str, address_title: &str, address_url: &str, c_id: &str) -> bool {
    match urls::save_url_form(address_id, address_title, address_url, c_id) {
        Ok(()) => true,
        Err(e) => {
            error!("保存失败！{:?}", e);
            false
        }
    }
}
#[tauri::command]
pub fn del_url_by_id(id: &str) -> bool {
    match urls::del_url_by_id(id) {
        Ok(()) => true,
        Err(e) => {
            error!("删除失败！{:?}", e);
            false
        }
    }
}
#[tauri::command]
pub fn del_class_by_id(id: &str) -> bool {
    match urls::del_class_by_id(id) {
        Ok(()) => true,
        Err(e) => {
            error!("删除失败！{:?}", e);
            false
        }
    }
}
#[tauri::command]
pub fn save_class(c_id: &str, c_name: &str) -> bool {
    match urls::save_class(c_id, c_name) {
        Ok(()) => true,
        Err(e) => {
            error!("保存失败！{:?}", e);
            false
        }
    }
}

#[tauri::command]
pub fn get_all_pass_class() -> Vec<pass::PassClass> {
    match pass::get_class() {
        Ok(list) => list,
        Err(e) => {
            error!("获取分类失败!{:?}", e);
            vec![]
        }
    }
}
#[tauri::command]
pub fn save_pass_item_form(
    pass_id: &str,
    pass_account: &str,
    pass_password: &str,
    pass_notes: &str,
) -> bool {
    match pass::save_pass_item_form(pass_id, pass_account, pass_password, pass_notes) {
        Ok(()) => {
            refresh_pass_cache();
            true
        }
        Err(e) => {
            error!("保存失败!{:?}", e);
            false
        }
    }
}
#[tauri::command]
pub fn refresh_pass_cache() -> bool {
    let bind = common::DB_CONNECT.lock().unwrap();
    let conn = bind.as_ref().unwrap();
    match pass::refresh_pass_cache(conn) {
        Ok(()) => true,
        Err(e) => {
            error!("刷新缓存失败!{:?}", e);
            false
        }
    }
}
#[tauri::command]
pub fn get_all_pass(key_word: &str) -> Vec<PassItem> {
    match pass::get_pass_items(key_word) {
        Ok(mut list) => {
            //对密码进行解密
            for i in &mut list{
                i.pass_password = common::decrypt_str(&i.pass_password);
            }
            list
        },
        Err(e) => {
            error!("获取密码列表失败!{:?}", e);
            vec![]
        }
    }
}
#[tauri::command]
pub fn apply_pass_gen_setting(setting: &str) -> bool {
    match pass::save_pass_gen_setting(setting) {
        Ok(()) => true,
        Err(e) => {
            error!("密码生成规则保存失败!{:?}", e);
            false
        }
    }
}
#[tauri::command]
pub fn common_get_setting_item(s_type: &str) -> Vec<common::ToolsSetting> {
    match common::get_setting_item(s_type) {
        Ok(item) => vec![item],
        Err(e) => {
            error!("获取配置项{}失败!{:?}", s_type, e);
            vec![]
        }
    }
}
#[tauri::command]
pub fn pass_gen_pass() -> String {
    pass::gen_pass()
}
#[tauri::command]
pub fn save_pass_class(c_id: &str, c_name: &str) -> bool {
    match pass::save_class(c_id, c_name) {
        Ok(()) => true,
        Err(e) => {
            error!("保存密码项失败!{:?}", e);
            false
        }
    }
}
#[tauri::command]
pub fn del_pass_item(pass_id: &str) -> bool {
    match pass::del_pass_item(pass_id) {
        Ok(()) => true,
        Err(e) => {
            error!("删除密码项失败!{:?}", e);
            false
        }
    }
}

#[tauri::command]
pub fn del_pass_class(c_id: &str) -> bool {
    match pass::del_pass_class(c_id) {
        Ok(()) => true,
        Err(e) => {
            error!("删除密码分类失败!{:?}", e);
            false
        }
    }
}

#[tauri::command]
pub fn todo_save(
    t_id: &str,
    t_title: &str,
    end_date: &str,
    remind_time: &str,
    repeat_rule: &str,
    rule_detail: &str,
    priority_level: &str,
    first_end_date: &str,
) -> bool {
    match todo::save(
        t_id,
        t_title,
        end_date,
        remind_time,
        repeat_rule,
        rule_detail,
        priority_level,
        first_end_date,
    ) {
        Ok(()) => true,
        Err(e) => {
            error!("保存待办失败!{:?}", e);
            false
        }
    }
}
#[tauri::command]
pub fn get_todo_list(t_state: &str) -> Vec<todo::Todo> {
    match todo::get_todo_list(t_state) {
        Ok(list) => list,
        Err(e) => {
            error!("获取todo列表失败!{:?}", e);
            vec![]
        }
    }
}
#[tauri::command]
pub fn done_todo(t_id: &str) -> bool {
    match todo::done_todo(t_id) {
        Ok(()) => true,
        Err(_) => false,
    }
}
#[tauri::command]
pub fn del_todo(t_id: &str) -> bool {
    match todo::del_todo(t_id) {
        Ok(()) => true,
        Err(_) => false,
    }
}
